package com.yuwei.module_cash_desk

import android.widget.CheckBox
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.lib_http.entity.CashDeskEntityItem
import com.yuwei.module_cash_desk.adapter.ChargeAdapter
import com.yuwei.module_cash_desk.databinding.ActivityBillDetailBinding
import com.yuwei.module_cash_desk.viewmodel.CashDeskViewModel
/**
 * 智慧园区-03-收银台-账单详情-V1.1-20250508-邓孟卓
 */
@Route(path = RouterPath.MODULE_BILL_DETAIL_ACTIVITY)
class BillDetailActivity : BaseMvvmActivity<ActivityBillDetailBinding>() {
    @Autowired
    lateinit var cashDesk: CashDeskEntityItem
    private val vm : CashDeskViewModel by viewModels()
    private val chargeAdapter = ChargeAdapter()
    var money = 0
    override fun initViews() {
        ARouter.getInstance().inject(this)
        binding.billDetailBack.setOnClickListener {
            finish()
        }
        vm.getChargeList()
        binding.projectName.text = cashDesk.projectName
        binding.payerName.text = cashDesk.payerName
        binding.contactNumber.text = cashDesk.contactNumber

        binding.rv.adapter = chargeAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        chargeAdapter.addChildClickViewIds(R.id.check)
        chargeAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.check ->{
                    val check = chargeAdapter.getViewByPosition(position, R.id.check) as CheckBox
                    chargeAdapter.data[position].isChecked = check.isChecked
                    allCheck()
                    jsMoney()
                }
            }
        }
        binding.checkAll.setOnClickListener {
            chargeAdapter.data.forEach {
                it.isChecked = binding.checkAll.isChecked
            }
            chargeAdapter.notifyDataSetChanged()
            jsMoney()
        }
        binding.pay.setOnClickListener {
            money = 0
            var flag = false
            chargeAdapter.data.forEach {
                if(it.isChecked){
                    flag = true
                    money += it.chargeAmount
                }
            }
            if(flag){
                ARouter.getInstance().build(RouterPath.MODULE_BILL_PAY_ACTIVITY)
                    .withSerializable("cashDesk", cashDesk)
                    .withInt("money", money)
                    .navigation()
            }else{
                ToastUtils.showLong("请选择需要支付的项目")
            }
        }
        binding.sf.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MODULE_ADD_CHARGE_ACTIVITY).navigation()
        }
    }
    override fun observeDataChange() {
        vm.getChargeListSuccess.observe(this){
            chargeAdapter.data.addAll(it)
            it.forEach {
                money += it.chargeAmount
            }
            binding.textView9.text = it[0].paymentTime
            binding.hj.text ="合计:￥${money}"
            binding.totalAmount.text = "￥${money}.00"
            chargeAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_bill_detail
    private fun allCheck() {
        var flag  = true
        chargeAdapter.data.forEach {
            if(!it.isChecked){
                flag = false
            }

        }
        binding.checkAll.isChecked = flag
    }
    fun jsMoney(){
        var sum = 0
        chargeAdapter.data.forEach {
            if(it.isChecked){
                sum+=it.chargeAmount
            }
        }
        binding.total.text = "￥${sum}.00"
    }
}